package edu.scau.mis.controller.user;

import edu.scau.mis.core.controller.ResultEntity;
import edu.scau.mis.core.redis.RedisCache;
import edu.scau.mis.rest.entity.RestCategory;
import edu.scau.mis.rest.entity.vo.RestCategoryVo;
import edu.scau.mis.rest.entity.vo.RestItemVoForCombo;
import edu.scau.mis.rest.service.RestCategoryService;
import edu.scau.mis.rest.service.RestGroupDiscountService;
import edu.scau.mis.rest.service.RestItemService;
import edu.scau.mis.sys.utils.HttpServletRequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class foodController {

    @Autowired
    RestCategoryService restCategoryService;

    @Autowired
    RestItemService restItemService;

    @Autowired
    RestGroupDiscountService restGroupDiscountService;

    @Autowired
    RedisCache redisCache;

    @Autowired
    HttpServletRequest httpServletRequest;

    @GetMapping("/leftmenu")
    public ResultEntity getLeftMenu(){
        List<RestCategory> restCategoryList = restCategoryService.queryAll();
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("leftmenu", restCategoryList);
        return ResultEntity.success("操作成功", responseData);
    }

    @GetMapping("/rightmenu")
    public ResultEntity getRightMenu(){
        List<RestCategoryVo> restCategoryVoList = restCategoryService.queryAllCategoryAndItem();
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("rightmenu", restCategoryVoList);
        return ResultEntity.success("操作成功", responseData);
    }

    @GetMapping("/menu")
    public ResultEntity getGroupDiscountMenu(){
        RestCategory restCategory = restCategoryService.queryCategoryByCategotyNo("C4");
        List<RestItemVoForCombo> restItemList = restItemService.queryComboByCategoryId(restCategory.getId());
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("menu", restItemList);
        return ResultEntity.success("操作成功", responseData);
    }

    @PostMapping("/dofood")
    public ResultEntity processingItem(@RequestBody Map<String, String> map){
        //获取请求头token
        String openid = HttpServletRequestUtils.getOpenid(httpServletRequest);

        String itemNo = map.get("itemNo");
        String method = map.get("method");

        BigDecimal totalAmount = restItemService.processItem(itemNo, method, openid);

        Map<String, Object> responseData = new HashMap<>();
        responseData.put("itemList", redisCache.getCacheList(redisCache.getListByIndex(openid, 0).toString()));
        responseData.put("totalAmount", totalAmount);
        return ResultEntity.success("操作成功", responseData);
    }

    @GetMapping("/clearShopCar")
    public ResultEntity clearShopCar(){
        //获取请求头token
        String openid = HttpServletRequestUtils.getOpenid(httpServletRequest);

        //清空餐位餐品列表
        redisCache.deleteObject(redisCache.getListByIndex(openid, 0).toString());
        return ResultEntity.success("操作成功");
    }

}
